// pages/me/foreverorder.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    foreverorder: [
      // {
      //   order_id:1,
      //   order_num:2018121201,
      //   order_name:'土豪卫生巾盒子',
      //   order_price:89,
      //   box_num:6,
      //   box_url:'/image/sf.jpg',
      //   sum_price:534,
      //   isWho:'本人',
      //   remain:5
      // },
      // {
      //   order_id: 2,
      //   order_num: 2018121202,
      //   order_name: '聚划算生巾盒子',
      //   order_price: 56,
      //   box_num: 6,
      //   box_url: '/image/sf.jpg',
      //   sum_price: 336,
      //   isWho: '妹妹',
      //   remain: 4
      // },
      // {
      //   order_id: 3,
      //   order_num: 2018121203,
      //   order_name: '聚便宜生巾盒子',
      //   order_price: 33,
      //   box_num: 6,
      //   box_url: '/image/sf.jpg',
      //   sum_price: 198,
      //   isWho: '姐姐',
      //   remain: 3
      // }
    ],
    generateorder:[],
    topTab: 0,
    // brand_arr: [],
    // brand_index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getForeverOrder();
    this.getGenerateOrder();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取永久订单
  getForeverOrder: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getForeverOrder',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        console.log(e);
        that.setData({
          foreverorder: e.data
        });
      }
    })
  },
  // 获取全部订单
  getGenerateOrder: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getGenerateOrder',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        console.log(e);
        that.setData({
          generateorder: e.data
        });
      }
    })
  },
  // 立即续费 生成订单
  ljXufei:function () {
    // wx.navigateTo({
    //   // 跳转传参算数
    //   url: '/pages/me/ImmediateRenewal'
    // })
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'generateOrder',
      method: 'GET',
      data: {
        openid: app.globalData.openid,
        box_id: that.data.foreverorder[0].box_id,
        // sum_box: that.data.foreverorder[0].sum_box,
        // sum_price: that.data.foreverorder[0].sum_price,
        menstrual_volume: that.data.foreverorder[0].menstrual_volume,
        brand: that.data.foreverorder[0].brand,
        address_id: that.data.foreverorder[0].address_id,
        pay_type: 1
      },
      success: function (e) {
        // console.log(e);
        if (e.data) {
          wx.navigateTo({
            // 跳转传参算数
            url: '/pages/me/ImmediateRenewal?id=' + e.data.id
          })
        }
      }
    })
  },
  // 立即付款
  ljPay:function(e){
    // console.log(e);
    if (e.currentTarget.dataset.order_id){
      wx.navigateTo({
        // 跳转传参算数
        url: '/pages/home/order?id=' + e.currentTarget.dataset.order_id + '&prohibit=1'
      })
    }
  },
  // 立即付款(续费)
  ljPayXf: function (e) {
    // console.log(e);
    if (e.currentTarget.dataset.order_id) {
      wx.navigateTo({
        // 跳转传参算数
        url: '/pages/me/ImmediateRenewal?id=' + e.currentTarget.dataset.order_id + '&prohibit=1'
      })
    }
  },
  changeTab: function (e) {
    // console.log(e);
    if (this.data.topTab === e.currentTarget.dataset.val) {
      return;
    } else {
      this.setData({
        topTab: e.currentTarget.dataset.val
      });
    }
  },
  // 删除订单按钮
  btnDel:function(e){
    // console.log(e);
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除此订单',
      showCancel: true,
      confirmColor: '#fc5871',
      success: function (res) {
        if (res.confirm) {
          that.orderDel(e.currentTarget.dataset.order_id);
        }
      }
    })
  },
  // 订单删除
  orderDel:function(orderId){
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'orderDel',
      method: 'GET',
      data: {
        order_id: orderId
      },
      success: function (e) {
        if (e.data.status == 1) {
          wx.showToast({
            title: '删除成功！',
            icon: 'success',
            duration: 800,
            success: function (res) {
              that.onShow();
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '删除失败！',
            confirmColor: '#fc5871'
          })
        }
      }
    })
  },
  // 更改品牌
  changeBrand:function(e){
    console.log(e);
    var that = this;
    if (this.data.foreverorder[0].brand_arr[e.detail.value] == this.data.foreverorder[0].brand){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请选择不同的品牌更换！',
        confirmColor: '#fc5871'
      });
    }else{
      wx.request({
        url: app.globalData.serverUrl + 'changeBoxBrand',
        method:'GET',
        data:{
          forever_id: that.data.foreverorder[0].id,
          curBrand: this.data.foreverorder[0].brand_arr[e.detail.value]
        },
        success:function(e){
          console.log(e);
          that.getForeverOrder();
        }
      })
    }
  }
})

